/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import javax.persistence.*;
import jpapractica.*;

/**
 *
 * @author master
 */
public class ActorManager {
    
    EntityManagerFactory emf;
    public EntityManager managerActor;
    
    public ActorManager(){
        emf= Persistence.createEntityManagerFactory("GRLUPU");
        managerActor= emf.createEntityManager();
    }
    
    public Actor create (int id, String name, String lastname, String picture,
                            String birthplace, int dia, int mes, int ano){
        
        Actor actor = new Actor(id, name, lastname, picture, birthplace,dia,mes,ano);
        
      
        managerActor.getTransaction().begin();
        managerActor.persist(actor);
        managerActor.getTransaction().commit();
       
        return actor;
        
    }
    public void update (int id, String name, String lastname, String picture,
                            String birthplace, int dia, int mes, int ano){
        Actor actor = managerActor.find(Actor.class, id);
        
        managerActor.getTransaction().begin();
        actor.setName(name);
        actor.setLastname(lastname);
        actor.setPicture(picture);
        actor.setBirthplace(birthplace);
        actor.setBirthdate(dia,mes,ano);
        
        managerActor.getTransaction().commit();
    }
    public void delete (int id){
        Actor actor = managerActor.find(Actor.class, id);
        
        managerActor.getTransaction().begin();
        managerActor.remove(actor);
        managerActor.getTransaction().commit();
    }
    public List<Actor> buscar(String palabra){
   
        Query busqueda =managerActor.createQuery("SELECT p FROM Person p WHERE p.name='"+palabra+"'");
        List<Actor> aux =busqueda.getResultList();
       
        return aux;

    }
    public List<Movie> buscarpeliculas(){
        Query busqueda =managerActor.createQuery("SELECT p FROM Movie p ");
        List<Movie> aux =busqueda.getResultList();
        Movie a= aux.get(0);
     
       // System.out.println(a.getName());
         return aux;
        
    }
    
    
            
    
}
